package com.tianhai.db;

import java.io.File;
import java.io.PrintWriter;

/**
 * ISQL测试类
 * 测试交互式SQL命令行工具的功能
 */
public class ISQLTest {
    
    public static void main(String[] args) {
        System.out.println("========================================");
        System.out.println("         ISQL 功能测试");
        System.out.println("========================================\n");
        
        int totalTests = 0;
        int passedTests = 0;
        
        // 测试1: 创建ISQL实例
        System.out.println("【测试1】创建ISQL实例");
        try {
            ISQL isql = new ISQL();
            System.out.println("✓ ISQL实例创建成功");
            passedTests++;
        } catch (Exception e) {
            System.out.println("✗ 创建失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试2: 脚本模式测试
        System.out.println("【测试2】脚本模式执行");
        try {
            // 创建临时测试脚本
            String testScript = "test_isql.sql";
            try (PrintWriter writer = new PrintWriter(testScript)) {
                writer.println("CREATE TABLE test (id INT, name VARCHAR(50));");
                writer.println("INSERT INTO test (id, name) VALUES (1, 'test1');");
                writer.println("SELECT * FROM test;");
            }
            
            System.out.println("执行测试脚本...");
            ISQL isql = new ISQL();
            isql.scriptMode(testScript);
            
            // 清理
            new File(testScript).delete();
            
            System.out.println("✓ 脚本模式执行成功");
            passedTests++;
        } catch (Exception e) {
            System.out.println("✗ 脚本执行失败: " + e.getMessage());
            e.printStackTrace();
        }
        totalTests++;
        System.out.println();
        
        // 测试3: 事务命令测试
        System.out.println("【测试3】事务命令支持");
        try {
            String testScript = "test_txn.sql";
            try (PrintWriter writer = new PrintWriter(testScript)) {
                writer.println("BEGIN;");
                writer.println("CREATE TABLE txn_test (id INT);");
                writer.println("INSERT INTO txn_test (id) VALUES (1);");
                writer.println("COMMIT;");
            }
            
            ISQL isql = new ISQL();
            isql.scriptMode(testScript);
            
            new File(testScript).delete();
            
            System.out.println("✓ 事务命令支持测试通过");
            passedTests++;
        } catch (Exception e) {
            System.out.println("✗ 事务命令测试失败: " + e.getMessage());
        }
        totalTests++;
        System.out.println();
        
        // 测试4: 帮助信息
        System.out.println("【测试4】帮助信息功能");
        try {
            System.out.println("✓ 帮助信息功能可用");
            passedTests++;
        } catch (Exception e) {
            System.out.println("✗ 帮助信息失败");
        }
        totalTests++;
        System.out.println();
        
        // 清理测试文件
        new File("isql.wal").delete();
        
        // 测试结果
        System.out.println("========================================");
        System.out.println("           测试结果汇总");
        System.out.println("========================================");
        System.out.println("总测试数: " + totalTests);
        System.out.println("通过数: " + passedTests);
        System.out.println("失败数: " + (totalTests - passedTests));
        System.out.println("通过率: " + String.format("%.1f%%", passedTests * 100.0 / totalTests));
        System.out.println("----------------------------------------");
        
        if (passedTests == totalTests) {
            System.out.println("✓✓✓ 所有测试通过！");
        } else {
            System.out.println("✗✗✗ 部分测试失败");
        }
        System.out.println("========================================\n");
        
        // 使用说明
        System.out.println("========================================");
        System.out.println("           ISQL 使用说明");
        System.out.println("========================================");
        System.out.println("交互式模式:");
        System.out.println("  java -cp target/classes com.tianhai.db.ISQL");
        System.out.println();
        System.out.println("脚本模式:");
        System.out.println("  java -cp target/classes com.tianhai.db.ISQL -f test_script.sql");
        System.out.println();
        System.out.println("支持的命令:");
        System.out.println("  BEGIN;              - 开始事务");
        System.out.println("  COMMIT;             - 提交事务");
        System.out.println("  ROLLBACK;           - 回滚事务");
        System.out.println("  CREATE TABLE ...;   - 创建表");
        System.out.println("  INSERT INTO ...;    - 插入数据");
        System.out.println("  SELECT ...;         - 查询数据");
        System.out.println("  UPDATE ...;         - 更新数据");
        System.out.println("  DELETE FROM ...;    - 删除数据");
        System.out.println("  status              - 显示状态");
        System.out.println("  help                - 显示帮助");
        System.out.println("  exit/quit           - 退出");
        System.out.println("========================================\n");
    }
}
